
package ma.ensias.healthme.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import ma.ensias.healthme.entities.Personne;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;


public class PersonneDao implements PersonneDaoInterface{

    HttpClient client = new DefaultHttpClient();;
    HttpPost post = new HttpPost(DaoUtils.URL+"personne.php");;

    public  PersonneDao() {
//        try {
//            HttpClient client = new DefaultHttpClient();
//            HttpPost post = new HttpPost(DaoUtils.URL+"personne.php");
//
//        } catch (Exception e) {
//            System.out.println("error");
//        }
    }

    public void insert(Personne personne) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(DaoUtils.URL+"personne.php");
        try {
            List<NameValuePair> donnes = new ArrayList<NameValuePair>(1);

            //assign data
            donnes.add(new BasicNameValuePair("requet", "insert"));
            donnes.add(new BasicNameValuePair("id", String.valueOf(personne.getId())));
            donnes.add(new BasicNameValuePair("nom", personne.getNom()));
            donnes.add(new BasicNameValuePair("prenom", personne.getPrenom()));
            donnes.add(new BasicNameValuePair("role", personne.getRole()));

            post.setEntity(new UrlEncodedFormEntity(donnes));
            client.execute(post);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        } catch (Exception e) {
            System.out.println("error");
        }
    }
    
    public void update(Personne personne) {
        try {
            List<NameValuePair> donnes = new ArrayList<NameValuePair>(1);

            //assign data
            donnes.add(new BasicNameValuePair("requet", "update"));
            donnes.add(new BasicNameValuePair("id", String.valueOf(personne.getId())));
            donnes.add(new BasicNameValuePair("nom", personne.getNom()));
            donnes.add(new BasicNameValuePair("prenom", personne.getPrenom()));
            donnes.add(new BasicNameValuePair("role", personne.getRole()));

            post.setEntity(new UrlEncodedFormEntity(donnes));
            client.execute(post);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void delete(Personne personne){
        this.delete(personne.getId());
    }

    public void delete(int id) {
        try {
            List<NameValuePair> donnes = new ArrayList<NameValuePair>(1);

            //assign data
            donnes.add(new BasicNameValuePair("requet", "delete"));
            donnes.add(new BasicNameValuePair("id", String.valueOf(id)));
            

            post.setEntity(new UrlEncodedFormEntity(donnes));
            client.execute(post);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	@Override
	public List<Personne> getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Personne getPersonne(int id) {
		/*
		 * A faire !!!!!!!!!!!!!!!!!!!!!!!!!
		 * 
		 */
		return null;
	}
}
